package com.xfly.app.ui;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ptmall.app.R;
import com.ptmall.app.base.BaseMvpActivity;
import com.ptmall.app.bean.AddressBean;
import com.ptmall.app.net.HttpMethods;
import com.ptmall.app.net.api.ApiDataRepository;
import com.ptmall.app.net.base.ApiNetResponse;
import com.ptmall.app.ui.adapter.OrderGoodAdapter;
import com.ptmall.app.ui.adapter.TjOrderGoodAdapter;
import com.ptmall.app.view.NoScrollListView;
import com.ptmall.app.view.TitleBarView;
import com.xfly.app.bean.CarBean;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class TjActivity extends BaseMvpActivity implements View.OnClickListener {

    TitleBarView title_bar;

    ApiDataRepository apiDataRepository = ApiDataRepository.getInstance();
    TjOrderGoodAdapter orderGoodAdapter;
    NoScrollListView listview;
    TextView name,phone,address,num,total;
    LinearLayout dz,dznull;
    String address_id;
    public static String ADDRESSID = "";
    ImageView xz_1,xz_2;
    String type = "money";
    Button lg_btn;
    EditText bz;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setView(R.layout.activity_tj);
    }

    @Override
    public void initViews() {
        title_bar = findViewById(R.id.title_bar);
        listview = findViewById(R.id.listview);
        dznull = findViewById(R.id.dznull);
        dz = findViewById(R.id.dz);
        name = findViewById(R.id.name);
        phone = findViewById(R.id.phone);
        address = findViewById(R.id.address);
        num = findViewById(R.id.num);
        total = findViewById(R.id.total);
        xz_1 = findViewById(R.id.xz_1);
        xz_2 = findViewById(R.id.xz_2);
        lg_btn = findViewById(R.id.lg_btn);
        bz = findViewById(R.id.bz);

        title_bar.setText("提交订单");
    }

    @Override
    public void initEvents() {
        orderGoodAdapter = new TjOrderGoodAdapter(getContext(),null);
        listview.setAdapter(orderGoodAdapter);
        title_bar.setOnTitleBarClickListener(new TitleBarView.TitleBarClickListener() {
            @Override
            public void leftClick() {
                finish();
            }

            @Override
            public void rightClick() {

            }
        });

        dz.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                Intent intent = new Intent(getActivity(), Web.class);
                intent.putExtra("title","地址列表");
                intent.putExtra("url", HttpMethods.BASE_SITE + "/cn/address/index/");
                startActivity(intent);
            }
        });

        dznull.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                Intent intent = new Intent(getActivity(), Web.class);
                intent.putExtra("title","地址列表");
                intent.putExtra("url", HttpMethods.BASE_SITE + "/cn/address/index/");
                startActivity(intent);
            }
        });

        lg_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                tj();
            }
        });
        xz_1.setOnClickListener(this);
        xz_2.setOnClickListener(this);

        clist();
    }

    @Override
    protected void onResume() {
        super.onResume();
        if(!TextUtils.isEmpty(ADDRESSID)){
            getAddressDetil(ADDRESSID);
        }
    }

    private void clist() {
        Map<String, String> map = new HashMap<>();
        map.put("mold", "2");
        apiDataRepository.clist(map, new ApiNetResponse<CarBean>(getContext()) {
            @Override
            public void onSuccess(CarBean data) {
                orderGoodAdapter.pushData(data.getGoods());
                num.setText("共"+data.getCount());
                total.setText("¥"+data.getTotal());
            }
        });
        apiDataRepository.getAddress(null, new ApiNetResponse<List<AddressBean>>() {
            @Override
            public void onSuccess(List<AddressBean> data) {
                if(data!=null&&data.size()>0){
                    address_id = data.get(0).id;
                    dz.setVisibility(View.VISIBLE);
                    dznull.setVisibility(View.GONE);
                    name.setText(data.get(0).name);
                    phone.setText(data.get(0).tel);
                    address.setText(data.get(0).address);
                }else {
                    dznull.setVisibility(View.VISIBLE);
                    dz.setVisibility(View.GONE);
                }
            }
        });
    }

    private void getAddressDetil(String id){
        Map<String,String> map = new HashMap<>();
        map.put("address_id",id);
        apiDataRepository.getAddressDetil(map, new ApiNetResponse<AddressBean>() {
            @Override
            public void onSuccess(AddressBean data) {

                address_id = data.id;
                dz.setVisibility(View.VISIBLE);
                dznull.setVisibility(View.GONE);
                name.setText(data.name);
                phone.setText(data.tel);
                address.setText(data.address);
            }
        });
        ADDRESSID = "";
    }

    private void tj(){
        if(TextUtils.isEmpty(address_id)){
            showMsg("请选择地址");
            return;
        }
        Map<String,String> map = new HashMap<>();
        map.put("payway",type);
        map.put("mold","2");
        map.put("intro",bz.getText().toString());
        map.put("address_id",address_id);
        apiDataRepository.tj(map, new ApiNetResponse<Object>() {
            @Override
            public void onSuccess(Object data) {
                showMsg("下单成功");
                Intent intent = new Intent(getActivity(), Web.class);
                intent.putExtra("url", HttpMethods.BASE_SITE + "/cn/orders/index/");
                startActivity(intent);
                finish();
            }
        });
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.xz_1:
                xz_1.setImageResource(R.drawable.xf_true);
                xz_2.setImageResource(R.drawable.xf_false);
                type = "money";
                break;
            case R.id.xz_2:
                xz_1.setImageResource(R.drawable.xf_false);
                xz_2.setImageResource(R.drawable.xf_true);
                type = "face";
                break;
        }
    }
}
